package com.buglife.video.convert.controller;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.buglife.video.convert.service.IVideoConvertService;
import com.buglife.video.convert.service.IVideoUploadService;
import com.buglife.video.convert.util.Conversor;
import com.buglife.video.convert.util.FileParameter;

@Controller
public class VUploadAndConvertController {
    
    @Autowired
    private IVideoConvertService videoConvertService;
    @Resource
    private IVideoUploadService  videoUploadService;
    
    @RequestMapping(value = "/upload")
    public ModelAndView uploadVideo(@RequestParam("videofile") MultipartFile file, HttpServletRequest request, HttpServletResponse response) {
        System.out.println("file origal name:::" + file.getOriginalFilename());
        videoUploadService.uploadVideo(file);
        videoConvertService.convert(FileParameter.VIDEO_PATH + "\\" + file.getOriginalFilename(), FileParameter.FLV_PATH + file.getOriginalFilename().substring(0, file.getOriginalFilename().indexOf("."))+ FileParameter.FLV_TYPE);
        return new ModelAndView("success");
    }
    
    @RequestMapping(value = "/getPercent")
    public void getPercent(HttpServletRequest request,HttpServletResponse response) throws IOException {
//        HashMap<String, String> model = new HashMap<String, String>();
//        model.put("percent", Conversor.percent);
//        return new ModelAndView("success",model);
        response.getWriter().print(Conversor.percent);
    }
}
